package com.nanxun.service.Impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanxun.constant.BusinessEnum;
import com.nanxun.constant.StoreConstants;
import com.nanxun.domain.IndexImg;
import com.nanxun.domain.Prod;
import com.nanxun.ex.handler.BusinessException;
import com.nanxun.feign.StoreProdFeign;
import com.nanxun.mapper.IndexImgMapper;
import com.nanxun.model.Result;
import com.nanxun.service.IndexImgService;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


@Service
@CacheConfig(cacheNames = "com.nanxun.service.Impl.IndexImgServiceImpl")
public class IndexImgServiceImpl extends ServiceImpl<IndexImgMapper, IndexImg> implements IndexImgService {

    @Resource
    private  IndexImgMapper indexImgMapper;


    @Resource
    private StoreProdFeign storeProdFeign;

    @Override
    @CacheEvict(key = StoreConstants.WX_INDEX_IMG_KEY)
    public Boolean saveIndexImg(IndexImg indexImg) {
        indexImg.setShopId(1L);
        indexImg.setCreateTime(new Date());
        //获取关联类型
        Integer type = indexImg.getType();
        if (-1==type){
            //说明：轮播图未关联商品
            indexImg.setProdId(null);
        }
        return indexImgMapper.insert(indexImg)>0;
    }

    @Override
    public IndexImg queryIndexImgInfoById(Long imgId) {
        //根据标识查询轮播图信息
        IndexImg indexImg = indexImgMapper.selectById(imgId);
        //获取轮播图关联类型
        Integer type = indexImg.getType();
        //判断关联商品
        if(0==type){
            //说明：当前轮播图已关联商品
            //获取关联商品的id
            Long prodId = indexImg.getProdId();
            //远程调用：根据商品id查询商品图片和名称，商品图片和名称在商品表里面，现在在门店服务模块，要调用商品业务模块下的商品服务接口，要用到远程调用openfeign
            Result<List<Prod>> result = storeProdFeign.getProdListByIds(Arrays.asList(prodId));
            //判断是否正确
            if (BusinessEnum.OPERATION_FAIL.getCode().equals(result.getCode())){
               //即：操作失败
               throw new BusinessException(result.getMsg());
            }
            //获取数据
            List<Prod> prods = result.getData();
            //判断集合是否有值
            if (CollectionUtil.isNotEmpty(prods) && prods.size() !=0){
                //获取商品对象
                Prod prod = prods.get(0);
                indexImg.setPic(prod.getPic());
                indexImg.setProdName(prod.getProdName());
            }
        }
        return indexImg;
    }

    @Override
    @CacheEvict(key = StoreConstants.WX_INDEX_IMG_KEY)
    public Boolean modifyIndexImg(IndexImg indexImg) {
        return indexImgMapper.updateById(indexImg)>0;
    }

    @Override
    @CacheEvict(key = StoreConstants.WX_INDEX_IMG_KEY)
    public Boolean removeIndexImgByIds(List<Long> imgIds) {
        return indexImgMapper.deleteBatchIds(imgIds)==imgIds.size()                                         ;
    }

    @Override
    @Cacheable(key= StoreConstants.WX_INDEX_IMG_KEY)
    public List<IndexImg> queryWxIndexImgList() {
        //没有传入条件，但是查询是有条件的
        return indexImgMapper.selectList(new LambdaQueryWrapper<IndexImg>()
                .eq(IndexImg::getStatus,1)//首先显示在小程序上面的轮播图状态是正常的，下线的肯定不能显示出来
                .orderByDesc(IndexImg::getSeq)
        );
    }
}
